﻿using System;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using Demo.Domain;

namespace Demo.Repository.Test
{
    [TestClass]
    public class UnitTest1
    {
        Demo.Repository.DemoContext db;

        [TestInitialize]
        public void Init()
        {
            db = new DemoContext();
        }

        [TestMethod]
        public void TestAddRoleAndUser()
        {
            var role3 = new Role { Name = "顾客", Sort = 3 };
            var role0 = new Role { Name = "超级管理员", Sort = 0 };
            var role1 = new Role { Name = "普通管理员", Sort = 1 };
            var role2 = new Role { Name = "前台", Sort = 2 };
            db.Role.Add(role3);
            db.Role.Add(role0);
            db.Role.Add(role1);
            db.Role.Add(role2);
            db.SaveChanges();
            role0 = db.Role.Where(r => r.Name == "超级管理员").FirstOrDefault();
            role1 = db.Role.Where(r => r.Name == "普通管理员").FirstOrDefault();
            role2 = db.Role.Where(r => r.Name == "前台").FirstOrDefault();
            if (role0 != null && role1 != null && role2 != null)
            {
                var user0 = new User { Name = "luoliang", RoleID = role0.RoleID, Password="123456" };
                var user1 = new User { Name = "asura", RoleID = role1.RoleID, Password = "123456" };
                var user2 = new User { Name = "demo", RoleID = role2.RoleID, Password = "123456" };
                db.User.Add(user0);
                db.User.Add(user1);
                db.User.Add(user2);
                db.SaveChanges();
            }

        }

        [TestMethod]
        public void TestAddModuleAndAction()
        {
            var module0 = new Module { Name = "系统管理", Sort = 0 };
            var module1 = new Module { Name = "用户管理", Sort = 1 };
            var module2 = new Module { Name = "角色管理", Sort = 2 };
            var module3 = new Module { Name = "权限管理", Sort = 3 };
            db.Module.Add(module0);
            db.Module.Add(module1);
            db.Module.Add(module2);
            db.Module.Add(module3);

            db.SaveChanges();

            module0 = db.Module.Where(m => m.Name == "系统管理").FirstOrDefault();
            module1 = db.Module.Where(m => m.Name == "用户管理").FirstOrDefault();
            module2 = db.Module.Where(m => m.Name == "角色管理").FirstOrDefault();
            module3 = db.Module.Where(m => m.Name == "权限管理").FirstOrDefault();
            if (module0 != null && module1 != null && module2 != null && module3 != null)
            {
                var action0 = new Demo.Domain.Action { Name = "定义站点名称", Sort = 0, ModuleID = module0 .ModuleID};
                var action1 = new Demo.Domain.Action { Name = "定义站点邮件", Sort = 1, ModuleID = module0.ModuleID };
                var action2 = new Demo.Domain.Action { Name = "定义站点拥有者", Sort = 2, ModuleID = module0.ModuleID };
                var action3 = new Demo.Domain.Action { Name = "定义站点域名", Sort = 3, ModuleID = module0.ModuleID };
                db.Action.Add(action0);
                db.Action.Add(action1);
                db.Action.Add(action2);
                db.Action.Add(action3);

                var action4 = new Demo.Domain.Action { Name = "增加用户", Sort = 4, ModuleID = module1.ModuleID };
                var action5 = new Demo.Domain.Action { Name = "删除用户", Sort = 5, ModuleID = module1.ModuleID };
                var action6 = new Demo.Domain.Action { Name = "修改用户", Sort = 6, ModuleID = module1.ModuleID };
                db.Action.Add(action4);
                db.Action.Add(action5);
                db.Action.Add(action6);

                var action7 = new Demo.Domain.Action { Name = "增加角色", Sort = 7, ModuleID = module2.ModuleID };
                var action8 = new Demo.Domain.Action { Name = "删除角色", Sort = 8, ModuleID = module2.ModuleID };
                var action9 = new Demo.Domain.Action { Name = "修改角色", Sort = 9, ModuleID = module2.ModuleID };
                var action10 = new Demo.Domain.Action { Name = "分派角色", Sort = 10, ModuleID = module2.ModuleID };
                var action11 = new Demo.Domain.Action { Name = "移除角色", Sort = 11, ModuleID = module2.ModuleID };
                db.Action.Add(action7);
                db.Action.Add(action8);
                db.Action.Add(action9);
                db.Action.Add(action10);
                db.Action.Add(action11);

                var action12 = new Demo.Domain.Action { Name = "权限权限", Sort = 12, ModuleID = module3.ModuleID };
                var action13 = new Demo.Domain.Action { Name = "删除权限", Sort = 13, ModuleID = module3.ModuleID };
                var action14 = new Demo.Domain.Action { Name = "修改权限", Sort = 14, ModuleID = module3.ModuleID };
                var action15 = new Demo.Domain.Action { Name = "分派权限", Sort = 15, ModuleID = module3.ModuleID };
                var action16 = new Demo.Domain.Action { Name = "移除权限", Sort = 16, ModuleID = module3.ModuleID };
                db.Action.Add(action12);
                db.Action.Add(action13);
                db.Action.Add(action14);
                db.Action.Add(action15);
                db.Action.Add(action16);
                db.SaveChanges();

                var aCount = db.Action.Count();
                var mCount = db.Module.Count();
            }

        }

        [TestMethod]
        public void TestAddRoleModule()
        {
            var role0 = db.Role.Where(r => r.Name == "超级管理员").FirstOrDefault();
            var modules = db.Module.ToList();
            foreach(var item in modules)
            {
                var rolemodule = new RoleModule { RoleID = role0.RoleID, ModuleID = item.ModuleID };
                db.RoleModule.Add(rolemodule);
                db.SaveChanges();
                var actions = db.Action.Where(a => a.ModuleID == item.ModuleID).ToList();
                foreach(var ite in actions)
                {
                    db.RoleModuleAction.Add(new RoleModuleAction {  ActionID = ite.ActionID, RoleModuleID = rolemodule.RoleModuleID});
                }
            }
            db.SaveChanges();
        }

        [TestMethod]
        public void TestAddRoleModuleAndAction()
        {

        }

        [TestMethod]
        public void TestAddUserModule()
        {
            var user0 = db.User.Where(u => u.Name == "luoliang").FirstOrDefault();
            var modules = db.Module.ToList();
            foreach (var item in modules)
            {
                var usermodule = new UserModule {  ModuleID = item.ModuleID, UserID = user0.UserID };
                db.UserModule.Add(usermodule);
                db.SaveChanges();
                var actions = db.Action.Where(a => a.ModuleID == item.ModuleID).ToList();
                foreach (var ite in actions)
                {
                    db.UserModuleAction.Add(new UserModuleAction { ActionID = ite.ActionID, UserModuleID = usermodule.UserModuleID });
                }
            }
            db.SaveChanges();
        }


        [TestMethod]
        public void TestAddUserModuleAndAction()
        {

        }
    }
}
